﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace DateTimeChecker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btClear_Click(object sender, EventArgs e)
        {
            txtDay.Clear();
            txtMonth.Clear();
            txtYear.Clear();
        }

        bool validate()
        {
            string ruleP = "^\\d+$";
            Regex regexP = new Regex(ruleP);
            if (!regexP.IsMatch(txtDay.Text))
            {
                MessageBox.Show("Input Data for Day is incorrect format!",
                "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error,
                MessageBoxDefaultButton.Button1);
                return false;
            }
            byte d = byte.Parse(txtDay.Text);
            if (d < 1 || d > 31)
            {
                MessageBox.Show("Input Data for Day is out of range!",
                "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error,
                MessageBoxDefaultButton.Button1);
                return false;
            }

            if (!regexP.IsMatch(txtMonth.Text))
            {
                MessageBox.Show("Input Data for Month is incorrect format!",
                "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error,
                MessageBoxDefaultButton.Button1);
                return false;
            }
            byte m = byte.Parse(txtMonth.Text);
            if (m < 1 || m > 12)
            {
                MessageBox.Show("Input Data for Month is out of range!",
                "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error,
                MessageBoxDefaultButton.Button1);
                return false;
            }

            if (!regexP.IsMatch(txtYear.Text))
            {
                MessageBox.Show("Input Data for Year is incorrect format!",
                "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error,
                MessageBoxDefaultButton.Button1);
                return false;
            }
            short y = short.Parse(txtYear.Text);
            if (y < 1000 || y > 3000)
            {
                MessageBox.Show("Input Data for Year is out of range!",
                "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error,
                MessageBoxDefaultButton.Button1);
                return false;
            }

            return true;

        }

        private void btCheck_Click(object sender, EventArgs e)
        {
            if (validate() == false)
            {
                return;
            }
            CheckDate();

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dialog = MessageBox.Show("Are you sure to exit?",
                "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialog == DialogResult.Yes)
            {
                Application.Exit();
            }
            else if (dialog == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        public byte DaysInMonth( byte month, short year)
        {
            
                if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                {
                    return 31;
                }

                if (month == 4 || month == 6 || month == 9 || month == 11)
                {
                    return 30;
                }
                if (month == 2)
                {
                    if ((year % 400) == 0){
                        return 29;
                    }
                    else if ((year % 100) == 0)
                    {
                        return 28;
                    }
                    else if ((year % 4) == 0)
                    {
                        return 29;
                    }
                    else if ((year % 4) != 0)
                    {
                        return 28;
                    }
                }
                return 0;
        }

        public bool IsValidDate(byte day, byte month, short year)
        {
            if (month >= 1 && month <= 12 && day >=1 && day <= DaysInMonth(month,year))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void CheckDate()
        {
            byte d = byte.Parse(txtDay.Text);
            byte m = byte.Parse(txtMonth.Text);
            short y = short.Parse(txtYear.Text);
            if (IsValidDate(d, m, y) == true)
            {
                string text1 = String.Format("{0}/{1}/{2} is correct date time!",d,m,y);                
                MessageBox.Show(text1,
                "Message",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button1);
            }
            else
            {
                string text2 = String.Format("{0}/{1}/{2} is NOT correct date time!", d, m, y);
                MessageBox.Show(text2,
                "Message",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button1);
            }
        }
    }
}
